Imports System.Data.SqlClient

Public Class DanhMuc
    Private sMa As Integer
    Private sTen As String
    Private sDvt As String
    Private nDgvl As Decimal
    Private nDgnc As Decimal
    Private nDgmtc As Decimal
    Private sMaHangMuc As String
    Private bTrangThai As Boolean

    Public Property Ma() As Integer
        Get
            Return sMa
        End Get
        Set(ByVal Value As Integer)
            sMa = Value
        End Set
    End Property

    Public Property Ten() As String
        Get
            Return sTen
        End Get
        Set(ByVal Value As String)
            sTen = Value
        End Set
    End Property

    Public Property Dvt() As String
        Get
            Return sDvt
        End Get
        Set(ByVal Value As String)
            sDvt = Value
        End Set
    End Property

    Public Property Dgvl() As Decimal
        Get
            Return nDgvl
        End Get
        Set(ByVal Value As Decimal)
            nDgvl = Value
        End Set
    End Property

    Public Property Dgnc() As Decimal
        Get
            Return nDgnc
        End Get
        Set(ByVal Value As Decimal)
            nDgnc = Value
        End Set
    End Property

    Public Property Dgmtc() As Decimal
        Get
            Return nDgmtc
        End Get
        Set(ByVal Value As Decimal)
            nDgmtc = Value
        End Set
    End Property

    Public Property MaHangMuc() As String
        Get
            Return sMaHangMuc
        End Get
        Set(ByVal Value As String)
            sMaHangMuc = Value
        End Set
    End Property

    Public Property TrangThai() As Boolean
        Get
            Return bTrangThai
        End Get
        Set(ByVal Value As Boolean)
            bTrangThai = Value
        End Set
    End Property

End Class

Public Class DanhMucF

    Public Function GetAll() As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DanhMuc_GetAll"

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function GetWhereMa(ByVal Ma As Integer) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DanhMuc_GetWhereMa"

            .Parameters.Add(New SqlClient.SqlParameter("@ma", SqlDbType.Decimal))
            .Parameters("@ma").Value = Ma

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function GetWhereMaHangMuc(ByVal MaHangMuc As String) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DanhMuc_GetWhereMaHangMuc"

            .Parameters.Add(New SqlClient.SqlParameter("@mahangmuc", SqlDbType.Char))
            .Parameters("@mahangmuc").Value = MaHangMuc

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function Save(ByVal DanhMuc As DanhMuc) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DanhMuc_Save"

            .Parameters.Add(New SqlClient.SqlParameter("@ma", SqlDbType.Decimal))
            .Parameters("@ma").Value = DanhMuc.Ma

            .Parameters.Add(New SqlClient.SqlParameter("@ten", SqlDbType.NVarChar))
            .Parameters("@ten").Value = DanhMuc.Ten

            .Parameters.Add(New SqlClient.SqlParameter("@dvt", SqlDbType.NVarChar))
            .Parameters("@dvt").Value = DanhMuc.Dvt

            .Parameters.Add(New SqlClient.SqlParameter("@dgvl", SqlDbType.Decimal))
            .Parameters("@dgvl").Value = DanhMuc.Dgvl

            .Parameters.Add(New SqlClient.SqlParameter("@dgnc", SqlDbType.Decimal))
            .Parameters("@dgnc").Value = DanhMuc.Dgnc

            .Parameters.Add(New SqlClient.SqlParameter("@dgmtc", SqlDbType.Decimal))
            .Parameters("@dgmtc").Value = DanhMuc.Dgmtc

            .Parameters.Add(New SqlClient.SqlParameter("@mahangmuc", SqlDbType.Char))
            .Parameters("@mahangmuc").Value = DanhMuc.MaHangMuc

            .Parameters.Add(New SqlClient.SqlParameter("@trangthai", SqlDbType.Bit))
            .Parameters("@trangthai").Value = DanhMuc.TrangThai

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function Delete(ByVal Ma As String) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DanhMuc_Delete"

            .Parameters.Add(New SqlClient.SqlParameter("@ma", SqlDbType.Decimal))
            .Parameters("@ma").Value = Ma

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function


End Class




